package com.zysf.library_boot.controller;

import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zysf.library_boot.entity.Admin;
import com.zysf.library_boot.entity.ReaderInfo;
import com.zysf.library_boot.ov.AdminOV;
import com.zysf.library_boot.ov.LoginRequest;
import com.zysf.library_boot.ov.ReaderInfoOV;
import com.zysf.library_boot.service.AdminService;
import com.zysf.library_boot.service.ReaderInfoService;
import com.zysf.library_boot.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")//localhost:8888/login
public class LoginController {
    @Autowired
    AdminService adminService;

    @Autowired
    ReaderInfoService readerInfoService;

    public static final String SIGNATURE = "123456";

    //登录
    @RequestMapping("/userLogin")
    public Result adminLogin(@RequestBody LoginRequest loginRequest){

        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();
        Integer userType = loginRequest.getUserType();
    if (userType == 1) {
        QueryWrapper<Admin> adminQw = new QueryWrapper();
        adminQw.eq("username", username);
        adminQw.eq("password", password);
        Admin adminRes = adminService.getOne(adminQw);

        if (adminRes != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", adminRes.getId());
            String token = JWTUtil.createToken(map, LoginController.SIGNATURE.getBytes());

            AdminOV adminOV = new AdminOV();
            BeanUtils.copyProperties(adminRes, adminOV);
            adminOV.setToken(token);
            return Result.ok(adminOV);
        }
    }else {
        QueryWrapper<ReaderInfo> readerQw = new QueryWrapper<>();
        readerQw.eq("username", username);
        readerQw.eq("password", password);
        ReaderInfo readerRes = readerInfoService.getOne(readerQw);

        if (readerRes != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", readerRes.getId());
            String token = JWTUtil.createToken(map, LoginController.SIGNATURE.getBytes());

            ReaderInfoOV readerInfoOV = new ReaderInfoOV();
            BeanUtils.copyProperties(readerRes, readerInfoOV);
            readerInfoOV.setToken(token);
            return Result.ok(readerInfoOV);
        }
    }
        return Result.fail("登录失败");
    }

    @PostMapping("/addRegisterReader")
    public Result addRegisterReader(@RequestBody ReaderInfo readerInfo) {
        boolean saved = readerInfoService.save(readerInfo);
        if (saved) {
            return Result.ok("注册成功");
        } else {
            return Result.fail("注册失败");
        }
    }

    @RequestMapping("/checkRegisterUsername")
    public Result checkRegisterUsername(@RequestParam String username) {
        QueryWrapper<ReaderInfo> qw = new QueryWrapper<>();
        qw.eq("username", username);
        ReaderInfo readerInfoRes = readerInfoService.getOne(qw);
        return Result.ok(readerInfoRes);
    }

    @RequestMapping("/checkRegisterRealname")
    public Result checkRegisterRealname(@RequestParam String realname) {
        QueryWrapper<ReaderInfo> qw = new QueryWrapper<>();
        qw.eq("realname", realname);
        ReaderInfo readerInfoRes = readerInfoService.getOne(qw);
        return Result.ok(readerInfoRes);
    }
}
